//Decrypts caeser cypher using frequiency analysys
public class brutus { 
 
        
        public static String Gwzyzx = "Kfwjbjqq yt dtz; fsi dtz; fsi dtz, Atqzrsnzx.\n"
					+ "Xywfyt, ymtz mfxy gjjs fqq ymnx bmnqj fxqjju\n"
					+ "Kfwjbjqq yt ymjj ytt, Xywfyt. Htzsywdrjs,\n"
					+ "Rd mjfwy itym otd ymfy djy ns fqq rd qnkj\n"
					+ "N ktzsi st rfs gzy mj bfx ywzj yt rj.\n"
					+ "N xmfqq mfaj lqtwd gd ymnx qtxnsl ifd\n"
					+ "Rtwj ymfs Thyfanzx fsi Rfwp Fsytsd\n"
					+ "Gd ymnx anqj htsvzjxy xmfqq fyyfns zsyt.\n"
					+ "Xt kfwj dtz bjqq fy tshj; ktw Gwzyzx' ytslzj\n"
					+ "Mfym fqrtxy jsiji mnx qnkj'x mnxytwd:\n"
					+ "Snlmy mfslx zuts rnsj jdjx; rd gtsjx btzqi wjxy,\n"
					+ "Ymfy mfaj gzy qfgtzw'i yt fyyfns ymnx mtzw.\n"
					+ "Fqfwzr. Hwd bnymns, 'Kqd, kqd, kqd!'\n";

	public static void main(String [] args) {         
		String message; 
		if(args.length == 0) {//then run on the demonstation cypher
			message	= Gwzyzx;
		}
		else {
			message = args[0];
		}		
                brutus_force(message);
		
	}

        //decripts based on given encripted message and key n
	public static String decaeser(String encripted_message, int key) {
	//shifting by 26 - key brings cypher back to normal state, if the key is correct and between 0 and 26 (in this case they will be)
        return caeser.caeser_cypher(encripted_message , 26 - key);
	}

	/*takes a cyphered string, attempts to decypher with all 26 possable keys, returns the best result
	acheved by finding the person corelation between a letter historigram of the string and the known 
	frequency distribution in the english language.
	works better on longer strings. Only works on english next. 
	(may sometmes work in other languages, who knows.)  */
	public static String brutus_force(String message) {

		//ENGAGE
		print("\n\n*******************\nBRUTUS FORCE ATTACK\n*******************\n\n");
		
		double max = 0;//used to compar and find max correlation
		int best_key = 0;//sores the best key
		//for each possable key, 0 - 25
		for(int i = 0; i < 26; i ++) {
			//print output in console for fun
			print("    Trying key " + i);

			//find the pearson correlation of the letter historigram to the known engish language letter distrobution
			double r = freq_anal.r(decaeser(message, i));
			//if the new value is greater than the max
			if (r > max) {
				//update the max and best key
				max = r;
				best_key = i;
			}
			
			print("    Correlation: " + r);
		}
		print("\n   Et Tu Brutus? ");//never too much melodrama
		
		String decypherd = decaeser(message, best_key);//this is probably the decyphered message
		print("\nBEST KEY: " + best_key);
	
		print("\n************CYPHERED**********\n");
		print(message);
		print("\n***********DECYPHERED*********\n");
		print(decypherd);

		
		return  decypherd;
	}

	//theres so much typing in this wacky language I'm going to wear out my flipping keyboard
	public static void print(String str) {
		System.out.println(str);//in the time it took me to type "System.out.println" I could have finised my bachleros degree and found a nice job in Califoinia, even started a famaly. 
	}	
}
